<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="paperName">
        <el-input
          v-model="queryParams.paperName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出版物" prop="publication">
        <el-input
          v-model="queryParams.publication"
          placeholder="请输入出版物"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="issueTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.issueTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="索引" prop="citation">
        <el-input
          v-model="queryParams.citation"
          placeholder="请输入索引"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师作者" prop="teacher.teacherName">
       <el-table-column label="学生作者" align="center" width="180">
              <template slot-scope="scope">
                <span>{{ concatArrayField(scope.row.students, 'studentName') }}</span>
              </template>
            </el-table-column>

        <el-select v-model="queryParams.teacherId" placeholder="请输入姓名" clearable size="small"

        filterable

        remote

        :remote-method="searchTeacher"

        :loading="loading">

        <el-option

        v-for="item in teacherOptions"

        :key="item.teacherId"

        :label="item.teacherName"

        :value="item.teacherId">

        </el-option>

        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:paper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:paper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:paper:remove']"
        >删除</el-button>
      </el-col>
       <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-upload"
                size="mini"
                :disabled="multiple"
                @click="handleSubmit"
                v-hasPermi="['edu:paper:submit']"
              >提交</el-button>
            </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['edu:paper:export']"
      >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="论文ID" align="center" prop="paperId" />
      <el-table-column label="名称" align="center" prop="paperName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="出版物" align="center" prop="publication" />
      <el-table-column label="时间" align="center" prop="issueTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="索引" align="center" prop="citation">
        <template slot-scope="scope">
          <dict-tag :options="citationOptions" :value="scope.row.citation"/>
        </template>
      </el-table-column>
      <el-table-column label="教师作者" align="center" prop="teacherId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:paper:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:paper:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改论文对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="paperName">
          <el-input v-model="form.paperName" placeholder="请输入名称" />
        </el-form-item>
         <el-form-item label="学生作者">
                  <el-select v-model="form.studentIds" multiple placeholder="请选择">
                    <el-option
                      v-for="item in studentOptions"
                      :key="item.studentId"
                      :label="item.studentName"
                      :value="item.studentId"
                    ></el-option>
                  </el-select>
                </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出版物" prop="publication">
          <el-input v-model="form.publication" placeholder="请输入出版物" />
        </el-form-item>
        <el-form-item label="时间" prop="issueTime">
          <el-date-picker clearable size="small"
            v-model="form.issueTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="索引" prop="citation">
          <el-input v-model="form.citation" placeholder="请输入索引" />
        </el-form-item>
        <el-form-item label="教师作者" prop="teacherId">
        <el-select v-model="form.teacherId" placeholder="请输入姓名" clearable size="small"

        filterable

        remote

        :remote-method="searchTeacher"

        :loading="loading">

        <el-option

        v-for="item in teacherOptions"

        :key="item.teacherId"

        :label="item.teacherName"

        :value="item.teacherId">

        </el-option>

        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPaper, getPaper, delPaper, addPaper, updatePaper, exportPaper,submitPaper} from "@/api/edu/paper";
import { listTeacher } from "@/api/edu/teacher";
import { listStudent } from "@/api/edu/student";
export default {
  name: "Paper",
  data() {
    return {
       // 学生选项
          studentOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 论文表格数据
      paperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      typeOptions: [],
      // 索引字典
      citationOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paperName: null,
        type: null,
        publication: null,
        issueTime: null,
        citation: null,
        teacherId: null
      },
      // 教师选项
      teacherOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("edu_paper_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("edu_paper_citation").then(response => {
      this.citationOptions = response.data;
    });
  },
  methods: {
     concatArrayField(arrayOfObjects, fieldName) {
        const fieldValues = arrayOfObjects.map(obj => obj[fieldName]);
        return fieldValues.join(', ');
},
  /** 按姓名查询教师 */

  searchTeacher(query){

  if (query !== '') {

  this.loading = true;

  setTimeout(() => {

  listTeacher({ teacherName: query }).then(response => {

  this.teacherOptions = response.rows.filter(item => {

  return item.teacherName.toLowerCase().indexOf(query.toLowerCase()) > -1;

  });

  this.loading = false;

  });

  }, 200);

  } else {

  this.teacherOptions = [];

  }

  },
    /** 查询论文列表 */
    getList() {
      this.loading = true;
      listPaper(this.queryParams).then(response => {
        this.paperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paperId: null,
        paperName: null,
        type: null,
        publication: null,
        issueTime: null,
        citation: null,
        teacherId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paperId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加论文";
      listTeacher().then(response => {
              this.teacherOptions = response.rows;
            });
            listStudent().then(response => {
                    this.studentOptions = response.rows;
                  });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const paperId = row.paperId || this.ids
      getPaper(paperId).then(response => {
        this.form = response.data;
           this.form.studentIds = response.data.students.map(student => student.studentId); // 提取 studentId 并赋值
        this.open = true;
        this.title = "修改论文";
      });
       listTeacher().then(response => {
              this.teacherOptions = response.rows;
            });
            listStudent().then(response => {
              this.studentOptions = response.rows;
            });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.paperId != null) {
            updatePaper(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaper(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paperIds = row.paperId || this.ids;
      this.$confirm('是否确认删除论文编号为"' + paperIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPaper(paperIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有论文数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPaper(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 提交按钮操作 */
        handleSubmit(row) {
          const paperIds = row.paperId || this.ids;
          this.$confirm('是否确认提交论文编号为"' + paperIds + '"的数据项?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function() {
              return submitPaper(paperIds);
            }).then(() => {
              this.getList();
              this.msgSuccess("删除成功");
            }).catch(() => {});
        },
  }
};
</script>
